﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Lists;
namespace ReportGenerators.QuanLyKho
{
    public partial class FrmThamSoBaoCaoTinhHinhTieuThuMatHang : BasicForms.FrmThamSoBaoCao
    {
        public FrmThamSoBaoCaoTinhHinhTieuThuMatHang()
        {
            InitializeComponent();
        }

        private void FrmThamSoBaoCaoTonKhoTheoHang_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;

            cmbKyBaoCao.SelectedIndex = 19;           

            #region cmbVatTuHangHoa
            DataView dvVatTuHangHoa = DanhMucVatTuHangHoa.GetAll().DataView;
            cmbVatTuHangHoa.DataSource = dvVatTuHangHoa;
            cmbVatTuHangHoa.DisplayColumn = "Ten";
            cmbVatTuHangHoa.ValueColumn = "ID";
            cmbVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbVatTuHangHoa.AddVisibleColumn("Ten", "Tên");
            cmbVatTuHangHoa.Buttons[1].Visible = false;
            #endregion
        }

        private void cmbVatTuHangHoa_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
                cmbVatTuHangHoa.EditValue = null;
        }

        protected override void PrepareDataForReport()
        {
            VatTuHangHoa selectedVatTuHangHoa = null;
            if (cmbVatTuHangHoa.EditValue is Guid)
            {
                selectedVatTuHangHoa = DanhMucVatTuHangHoa.Instance.GetByID((Guid)cmbVatTuHangHoa.EditValue);
            }
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            DataSet ds = new DataSet();
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = GeneralLibs.Config.TimeOut;
                SqlDataAdapter da = null;

                cmd.CommandText = "Select_ThongTinDoanhNghiep";
                cmd.Parameters.Clear();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");

                DataTable dtChiTietTinhHinhTieuThuMatHang = new DataTable("ChiTietTinhHinhTieuThuMatHang");
                dtChiTietTinhHinhTieuThuMatHang.Columns.Add("KyBaoCao", typeof(string));
                dtChiTietTinhHinhTieuThuMatHang.Rows.Add(dtChiTietTinhHinhTieuThuMatHang.NewRow());
                dtChiTietTinhHinhTieuThuMatHang.Rows[0]["KyBaoCao"] = _KyBaoCao.Ten;
                ds.Tables.Add(dtChiTietTinhHinhTieuThuMatHang);

                DataTable dtKhoanMucTinhHinhTieuThuMatHang = new DataTable("KhoanMucTinhHinhTieuThuMatHang");
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("TenKho", typeof(string));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("MaVatTuHangHoa", typeof(string));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("TenVatTuHangHoa", typeof(string));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("DonViTinh", typeof(string));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("SoLuongDauKy", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("GiaTriDauKy", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("SoLuongNhap", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("GiaTriNhap", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("SoLuongXuat", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("GiaTriXuat", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("SoLuongCuoiKy", typeof(decimal));
                dtKhoanMucTinhHinhTieuThuMatHang.Columns.Add("GiaTriCuoiKy", typeof(decimal));
                ds.Tables.Add(dtKhoanMucTinhHinhTieuThuMatHang);

                foreach (VatTuHangHoa vatTuHangHoa in DanhMucVatTuHangHoa.Instance)
                {
                    if (selectedVatTuHangHoa == null || vatTuHangHoa.ID == selectedVatTuHangHoa.ID)
                    {
                        foreach (Kho kho in DanhMucKho.Instance)
                        {
                            DataRow khoanMucRow = dtKhoanMucTinhHinhTieuThuMatHang.NewRow();
                            khoanMucRow["TenKho"] = kho.Ten;
                            khoanMucRow["MaVatTuHangHoa"] = vatTuHangHoa.Ma;
                            khoanMucRow["TenVatTuHangHoa"] = vatTuHangHoa.Ten;
                            khoanMucRow["DonViTinh"] = vatTuHangHoa.DonViTinh;

                            TonKho tonDauKy = TonKho.CalculateTonTuThoiDiemKhoaSoGanNhatDenThoiDiemNhapVaoTheoKho(cmd, _KyBaoCao.ThoiDiemDau, kho.ID, vatTuHangHoa.ID);
                            khoanMucRow["SoLuongDauKy"] = tonDauKy.SoLuong;
                            khoanMucRow["GiaTriDauKy"] = tonDauKy.ThanhTien;

                            TonKho phatSinhNhap = TonKho.CalculatePhatSinhNhap(cmd, _KyBaoCao.ThoiDiemDau, _KyBaoCao.ThoiDiemCuoi, kho.ID, vatTuHangHoa.ID);
                            khoanMucRow["SoLuongNhap"] = phatSinhNhap.SoLuong;
                            khoanMucRow["GiaTriNhap"] = phatSinhNhap.ThanhTien;

                            TonKho phatSinhXuat = TonKho.CalculatePhatSinhXuat(cmd, _KyBaoCao.ThoiDiemDau, _KyBaoCao.ThoiDiemCuoi, kho.ID, vatTuHangHoa.ID);
                            khoanMucRow["SoLuongXuat"] = phatSinhXuat.SoLuong;
                            khoanMucRow["GiaTriXuat"] = phatSinhXuat.ThanhTien;

                            TonKho tonCuoiKy = TonKho.CalculateTonTuThoiDiemKhoaSoGanNhatDenThoiDiemNhapVaoTheoKho(cmd, _KyBaoCao.ThoiDiemCuoi, kho.ID, vatTuHangHoa.ID);
                            khoanMucRow["SoLuongCuoiKy"] = tonCuoiKy.SoLuong;
                            khoanMucRow["GiaTriCuoiKy"] = tonCuoiKy.ThanhTien;

                            //if (!chkChiHienThiHangTon.Checked || tonDauKy.SoLuong > 0 || tonCuoiKy.SoLuong > 0 || phatSinhXuat.SoLuong > 0 || phatSinhNhap.SoLuong > 0)
                            dtKhoanMucTinhHinhTieuThuMatHang.Rows.Add(khoanMucRow);
                        }  
                    }
                                      
                }

                _Report = new ReportTemplates.QuanLyKho.TinhHinhTieuThuMatHang.RptTinhHinhTieuThuMatHang();
                _Report.SetDataSource(ds);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
            }
        }

        protected override void PrintReport()
        {
            if (_Report != null)
            {
                ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(_Report, "Tình hình tiêu thụ thuốc");
                frm.Show();
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {

        }
    }
}
